package com.hongxun.modules.eys.controller;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hongxun.common.utils.*;
import com.hongxun.modules.api.service.BDataDictionaryService;
import com.hongxun.modules.bss.entity.*;
import com.hongxun.modules.bss.service.DPictureService;
import com.hongxun.modules.bss.service.DWorkContentService;
import com.hongxun.modules.eys.entity.*;
import com.hongxun.modules.eys.service.*;
import com.hongxun.modules.sys.controller.AbstractController;
import com.hongxun.modules.sys.entity.SysRoleEntity;
import com.hongxun.modules.sys.service.SysRoleDeptService;
import com.hongxun.modules.sys.service.SysRoleService;
import com.hongxun.modules.sys.service.SysUserRoleService;
import com.lowagie.text.*;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 验收项目表
 *
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-09-18 14:14:27
 */
@RestController
@RequestMapping("/eys/epbase")
public class EPBaseController extends AbstractController {
    @Value("${com.hongxun.IMAGEROTT}")
    private String ROOT;
    @Autowired
    private EPBaseService ePBaseService;
    @Autowired
    private SysRoleDeptService sysRoleDeptService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private EPointService ePointService;
    @Autowired
    private ECrossCostService eCrossCostService;
    @Autowired
    private DPictureService dPictureService;
    @Autowired
    private EProblemexistService eProblemexistService;
    @Autowired
    private BDataDictionaryService bDataDictionaryService;
    @Autowired
    private EPRangeService ePRangeService;
    @Autowired
    private DWorkContentService dWorkContentService;
    @Autowired
    private EWorkContentService eWorkContentService;
    @Autowired
    private SysRoleService sysRoleService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("eys:epbase:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        String wheres = " 1=1 ";
        if (query.get("pBType") != null && !query.get("pBType").toString().equals("")) {
            wheres += " and  p_b_Type = '" + query.get("pBType").toString() + "' ";
        }
        if (query.get("pBHive") != null && !query.get("pBHive").toString().equals("")) {
            wheres += " and  p_b_hive = '" + query.get("pBHive").toString() + "' ";
        }
        if (query.get("pBHive") != null && !query.get("pBHive").toString().equals("")) {
            wheres += " and  p_b_hive = '" + query.get("pBHive").toString() + "' ";
        }
        if (query.get("pBState") != null && !query.get("pBState").toString().equals("")) {
            wheres += " and  p_b_state = '" + query.get("pBState").toString() + "' ";
        }
        if (query.get("pBName") != null && !query.get("pBName").toString().equals("")) {
            wheres += " and  p_b_name like '%" + query.get("pBName").toString() + "%' ";
        }
        if (query.get("state") != null && query.get("state").toString().equals("isdelno")) {
            wheres += " and  p_b_datastate != '删除' ";
        }
        boolean myprio=false;
        if (query.get("ourid") != null && query.get("ourid").toString().equals("isok")) {
            List<Long> roleIdList = sysUserRoleService.queryRoleIdList(getUserId());
            String deptId = "";

            for (Long id : roleIdList) {
                SysRoleEntity sysRoleEntity = sysRoleService.queryObject(id);
                if(sysRoleEntity.getRemark().equals("是"))
                {
                    myprio=true;
                }
                List<Long> deptIdList = sysRoleDeptService.queryDeptIdList(id);
                for (Long sid : deptIdList) {
                    deptId += sid + ",";
                }
            }
            if (deptId.length() > 0) {
                deptId = deptId.substring(0, deptId.length() - 1);
                wheres += " and  p_b_orgid in (" + deptId + ")";
            } else {
                wheres += " and  p_b_orgid in ( -1 )";
            }

        }
        if(!myprio)
        {
            wheres += "and  FIND_IN_SET('" + getUserId() + "',p_b_collectid)";
        }

        query.put("filterSql", wheres + " ");
        List<RequstEBaseEntity> eBaseEntities = new ArrayList<>();
        List<EPBaseEntity> ePBaseList = ePBaseService.queryList(query);
        for (EPBaseEntity e : ePBaseList) {
            Map<String, Object> map = new HashMap<>();
            map.put("filterSql", "projectguid = '" + e.getGuid() + "'");
            RequstEBaseEntity r = new RequstEBaseEntity();
            BeanUtils.copyProperties(e, r);
            int i = eProblemexistService.queryTotal(map);
            r.setProblemCount(i);
            eBaseEntities.add(r);
        }
        int total = ePBaseService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(eBaseEntities, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    @RequestMapping("/select")
    public R select(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        String wheres = " 1=1 ";
        wheres += " and  p_b_datastate != '删除' ";

        boolean myprio=false;
        if (query.get("ourid") != null && query.get("ourid").toString().equals("isok")) {
            List<Long> roleIdList = sysUserRoleService.queryRoleIdList(getUserId());
            String deptId = "";

            for (Long id : roleIdList) {
                SysRoleEntity sysRoleEntity = sysRoleService.queryObject(id);
                if(sysRoleEntity.getRemark().equals("是"))
                {
                    myprio=true;
                }
                List<Long> deptIdList = sysRoleDeptService.queryDeptIdList(id);
                for (Long sid : deptIdList) {
                    deptId += sid + ",";
                }
            }
            if (deptId.length() > 0) {
                deptId = deptId.substring(0, deptId.length() - 1);
                wheres += " and  p_b_orgid in (" + deptId + ")";
            } else {
                wheres += " and  p_b_orgid in ( -1 )";
            }

        }
        if(!myprio)
        {
            wheres += "and  FIND_IN_SET('" + getUserId() + "',p_b_collectid)";
        }
        query.put("filterSql", wheres + " ");
        List<EPBaseEntity> ePBaseList = ePBaseService.queryList(query);

        return R.ok().put("returnlist", ePBaseList);
    }

    @RequestMapping("/mapselect")
    public R mapselect(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        String wheres = " 1=1 ";
        wheres += " and  p_b_datastate != '删除' ";
        wheres += " and  p_b_state != '30' ";

        if (query.get("ourid") != null && query.get("ourid").toString().equals("isok")) {
            List<Long> roleIdList = sysUserRoleService.queryRoleIdList(getUserId());
            String deptId = "";

            for (Long id : roleIdList) {
                List<Long> deptIdList = sysRoleDeptService.queryDeptIdList(id);
                for (Long sid : deptIdList) {
                    deptId += sid + ",";
                }
            }
            if (deptId.length() > 0) {
                deptId = deptId.substring(0, deptId.length() - 1);
                wheres += " and  p_b_orgid in (" + deptId + ")";
            } else {
                wheres += " and  p_b_orgid in ( -1 )";
            }

        }

        query.put("filterSql", wheres + " ");
        List<EPBaseEntity> ePBaseList = ePBaseService.queryList(query);

        return R.ok().put("returnlist", ePBaseList);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{guid}")
    @RequiresPermissions("eys:epbase:info")
    public R info(@PathVariable("guid") String guid) {
        EPBaseEntity ePBase = ePBaseService.queryObject(guid);

        return R.ok().put("ePBase", ePBase);
    }

    /**
     * 信息
     */
    @RequestMapping("/guidang/{guid}")
    @RequiresPermissions("eys:epbase:info")
    public R guidang(@PathVariable("guid") String guid) {
        EPBaseEntity ePBase = ePBaseService.queryObject(guid);
        ePBase.setPBState("30");
        ePBase.setPBCollectid(ePBase.getPBCollectid().replace("\"","").replace("[","").replace("]",""));
        ePBaseService.update(ePBase);
        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("eys:epbase:save")
    public R save(@RequestBody EPBaseEntity ePBase) {
        ePBase.setPBCollectid(ePBase.getPBCollectid().replace("\"","").replace("[","").replace("]",""));

        ePBaseService.save(ePBase);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("eys:epbase:update")
    public R update(@RequestBody EPBaseEntity ePBase) {
        ePBase.setPBCollectid(ePBase.getPBCollectid().replace("\"","").replace("[","").replace("]",""));

        ePBaseService.update(ePBase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("eys:epbase:delete")
    public R delete(@RequestBody String[] guids) {
        ePBaseService.deleteBatch(guids);

        return R.ok();
    }


    @RequestMapping("/exports")
    public void exports(HttpServletRequest request, HttpServletResponse response) throws IOException, DocumentException {
        String guid = request.getParameter("guid");
        EPBaseEntity ePBase = ePBaseService.queryObject(guid);

        response.setContentType("application/msword;charset=utf-8");
        try {
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(((ePBase.getPBName() == null ? "" : ePBase.getPBName()) + "项目验收报告.doc").getBytes(), "iso-8859-1"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        Document document = new Document();
        BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

        RtfWriter2.getInstance(document, response.getOutputStream());
        Font titleFontye = new Font(bfChinese, 8, Font.BOLD);
        HeaderFooter footer = new HeaderFooter(new Phrase("页码：", titleFontye), true);
        footer.setAlignment(1);
        footer.setBorder(Rectangle.NO_BORDER);
        document.setFooter(footer);
        document.open();
        setexports(document, bfChinese, ePBase);

        document.close();


    }

    @RequestMapping("/exportsimg")
    public void exportsimg(HttpServletRequest request, HttpServletResponse response) throws IOException, DocumentException {
        String guid = request.getParameter("guid");
        EPBaseEntity ePBase = ePBaseService.queryObject(guid);

        response.setContentType("application/msword;charset=utf-8");
        try {
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(((ePBase.getPBName() == null ? "" : ePBase.getPBName()) + "项目验收照片.doc").getBytes(), "iso-8859-1"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        Document document = new Document();
        BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

        RtfWriter2.getInstance(document, response.getOutputStream());
        Font titleFontye = new Font(bfChinese, 8, Font.BOLD);
        HeaderFooter footer = new HeaderFooter(new Phrase("页码：", titleFontye), true);
        footer.setAlignment(1);
        footer.setBorder(Rectangle.NO_BORDER);
        document.setFooter(footer);
        document.open();
        setexportsimg(document, bfChinese, ePBase);

        document.close();


    }

    public void setexports(Document document, BaseFont bfChinese, EPBaseEntity ePBase) throws DocumentException, IOException {


        //表题字体
        Font titleFont = new Font(bfChinese, 11, Font.BOLD);
        //表格字体
        Font contextFont = new Font(bfChinese, 9, Font.NORMAL);
        // 签字字体
        Font qinFont = new Font(bfChinese, 10, Font.NORMAL);
        Paragraph title = new Paragraph("项目验收报告");
        title.setAlignment(Element.ALIGN_CENTER);
        title.setFont(titleFont);
        document.add(title);
        Paragraph titletow = new Paragraph("工程项目分类：" + "                                        验收时间 ：     年    月   日");
        titletow.setAlignment(Element.ALIGN_CENTER);
        titletow.setFont(qinFont);
        document.add(titletow);
        List<MyContent> myContentlistwt = dWorkContentService.getContentWt(ePBase.getGuid());
        addbgtable(document, ePBase, contextFont, myContentlistwt);
    }

    private void addbgtable(Document document, EPBaseEntity ePBase, Font contextFont, List<MyContent> myContentlistwt) throws DocumentException, IOException {
        String yinghan = "";
        int mm = 1;
        for (int i = 0; i < myContentlistwt.size(); i++) {
            if (i == 0) {
                yinghan += "(" + mm + ")" + myContentlistwt.get(i).getpType() + ":" + myContentlistwt.get(i).getMx() + ";";
                mm++;
            } else {
                if (myContentlistwt.get(i).getpType().equals(myContentlistwt.get(i - 1).getpType())) {
                    yinghan += myContentlistwt.get(i).getMx() + ";";
                } else {
                    yinghan += "\n";
                    yinghan += "(" + mm + ")" + myContentlistwt.get(i).getpType() + ":" + myContentlistwt.get(i).getMx() + ";";
                    mm++;
                }
            }
        }


        String wheres = " 1=1 ";
        wheres += " and  p_guid = '" + ePBase.getGuid() + "' ";
        Map<String, Object> mapaPOther = new HashMap<>();
        mapaPOther.put("filterSql", wheres + " ");
        List<EPRangeEntity> ePRangeList = ePRangeService.queryList(mapaPOther);
        String gznr="";
        gznr= getContentysxlandsb(gznr,ePRangeList);

        Table table = new Table(5);
        int[] withs = {1, 2, 2, 3, 2};
        table.setWidths(withs);
        /** 表格所占页面宽度 author:yyli Sep 15, 2010 */
        table.setWidth(90);
        /** 居中显示 author:yyli Sep 15, 2010 */
        table.setAlignment(Element.ALIGN_CENTER);
        /** 自动填满 author:yyli Sep 15, 2010 */
        table.setAutoFillEmptyCells(true);
        OutPutUtils.addcell(table, "工程名称: " + ePBase.getPBName(), 0, 5, false, contextFont);
        OutPutUtils.addcell(table, "施工单位: ", 0, 3, false, contextFont);
        OutPutUtils.addcell(table, "施工负责人: ", 0, 2, false, contextFont);
        OutPutUtils.addcell(table, "开工时间:       年    月   日 ", 0, 3, false, contextFont);
        OutPutUtils.addcell(table, "竣工时间:       年    月   日 ", 0, 2, false, contextFont);
        OutPutUtils.addcell(table, "组织验收部门:  ", 0, 3, false, contextFont);
        OutPutUtils.addcell(table, "验收人: ", 0, 2, false, contextFont);
        OutPutUtils.addcell(table, "参加部门 ", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, "姓名 ", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "参加部门 ", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "姓名  ", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, " ", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, " ", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, " ", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "  ", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, " ", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, " ", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, " ", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "  ", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "验收情况", 3, 0, true, contextFont);

        OutPutUtils.addcell(table, "主要工程量及技术参数：\n" +gznr , 0, 4, false, contextFont);

        OutPutUtils.addcell(table, "缺陷及处理意见：\n" + yinghan, 0, 4, false, contextFont);
        OutPutUtils.addcell(table, "验收人员：\n\n", 0, 4, false, contextFont);

        OutPutUtils.addcell(table, "验收复查", 2, 0, true, contextFont);
        OutPutUtils.addcell(table, "复查情况：\n\n", 0, 4, false, contextFont);
        OutPutUtils.addcell(table, "复查人员：\n\n                                                      时间：    年   月   日", 0, 4, false, contextFont);

        OutPutUtils.addcell(table, "验收评价:\n\n  ", 0, 5, false, contextFont);
        OutPutUtils.addcell(table, "备注: \n\n ", 0, 5, false, contextFont);

        document.add(table);

    }
    private String getContentysxlandsb(String counts, List<EPRangeEntity> aPRangeList) {

        for (EPRangeEntity aPRangeEntity : aPRangeList) {
            List<MyContent> myContentxlList = dWorkContentService.getContentysxl(aPRangeEntity.getGuid());
            List<MyContent> myContentsbList = dWorkContentService.getContentyssb(aPRangeEntity.getGuid());
            Map<String, Object> mapaPOther = new HashMap<>();
            String wheres = " 1=1 ";
            wheres += " and  p_guid = '" + aPRangeEntity.getPGuid() + "' ";
            wheres += " and  w_c_content like '" + aPRangeEntity.getPRName() + "%' ";
            mapaPOther.put("filterSql", wheres + " ");
            List<EWorkContentEntity> dWorkContentList = eWorkContentService.queryList(mapaPOther);


            if (myContentxlList.size() + myContentsbList.size() + dWorkContentList.size() > 0) {
                counts += "【" + aPRangeEntity.getPRName() + "情况】\n";
            }
            else
            {
                counts += "【" + aPRangeEntity.getPRName() + "情况】\n";
                counts += "   无 \n";
            }




            double gtzl=0;
            String  gtzlstr="";
            double gtxl=0;
            String  gtxlstr="";
            double gtlj=0;
            String  gtljstr="";
            double gtcc=0;
            String  gtccstr="";
            double dxjs=0;
            String  dxjsstr="";
            double dxlj=0;
            String  dxljstr="";
            double dxcc=0;
            String  dxccstr="";
            double lxaz=0;
            String  lxazstr="";

            List<String> listsbname = new ArrayList<String>();

            for (MyContent myContent : myContentsbList) {
                boolean hss= false;
                for(String sbname :listsbname) {
                    if(sbname.equals(myContent.getpType()))
                    {
                        hss=true;
                    }
                }
                if(!hss) {
                    listsbname.add(myContent.getpType());
                }
            }


            for (MyContent myContent : myContentxlList) {
                if (myContent.getpName().equals(aPRangeEntity.getPRName())
                        && myContent.getpType().equals("杆塔")
                        && myContent.getpPWorktype().equals("组立")) {
                    gtzlstr+=myContent.getMx()+";";
                    gtzl+=myContent.getZl();
                }
                if (myContent.getpName().equals(aPRangeEntity.getPRName())
                        && myContent.getpType().equals("杆塔")
                        && myContent.getpPWorktype().equals("修理")) {
                    gtxlstr+=myContent.getMx()+";";
                    gtxl+=myContent.getZl();
                }
                if (myContent.getpName().equals(aPRangeEntity.getPRName())
                        && myContent.getpType().equals("杆塔")
                        && myContent.getpPWorktype().equals("利旧")) {
                    dxljstr+=myContent.getMx()+";";
                    dxlj+=myContent.getZl();
                }
                if (myContent.getpName().equals(aPRangeEntity.getPRName())
                        && myContent.getpType().equals("杆塔")
                        && myContent.getpPWorktype().equals("拆除")) {
                    gtccstr+=myContent.getMx()+";";
                    gtcc+=myContent.getZl();
                }
                if (myContent.getpName().equals(aPRangeEntity.getPRName())
                        && myContent.getpType().equals("导线")
                        && myContent.getpPWorktype().equals("架设")) {
                    dxjsstr+=myContent.getMx()+";";
                    dxjs+=myContent.getZl();
                }
                if (myContent.getpName().equals(aPRangeEntity.getPRName())
                        && myContent.getpType().equals("导线")
                        && myContent.getpPWorktype().equals("利旧")) {
                    dxljstr+=myContent.getMx()+";";
                    dxlj+=myContent.getZl();
                }
                if (myContent.getpName().equals(aPRangeEntity.getPRName())
                        && myContent.getpType().equals("导线")
                        && myContent.getpPWorktype().equals("拆除")) {
                    dxccstr+=myContent.getMx()+";";
                    dxcc+=myContent.getZl();
                }

            }
            int i=1;
            int j=1;
            if(myContentxlList.size()>0) {

                counts += i+" 配网线路\n";
                i++;
                if(gtzl+gtcc+gtlj+gtxl>0)
                {
                    counts += "("+j+")杆塔情况：\n";
                    j++;
                    if(gtzl>0)
                    {
                        counts += "杆塔组立  合计："+(int)gtzl+"根，其中 " + gtzlstr+"\n";
                    }
                    if(gtxl>0)
                    {
                        counts += "杆塔修理  合计："+(int)gtxl+"根，其中 " + gtxlstr+"\n";
                    }
                    if(gtlj>0)
                    {
                        counts += "杆塔利旧 合计："+(int)gtlj+"根，其中 " +gtljstr+"\n";
                    }
                    if(gtcc>0)
                    {
                        counts += "杆塔拆除  合计："+(int)gtcc+"根，其中 " +gtccstr+"\n";
                    }

                }
                if(dxjs+dxcc+dxlj>0)
                {
                    counts += "("+j+")导线情况：\n";
                    j++;
                    if(dxjs>0)
                    {
                        counts += "导线架设  合计："+dxjs+"米，其中 " +dxjsstr+"\n";
                    }
                    if(dxlj>0)
                    {
                        counts += "导线利旧  合计："+dxlj+"米，其中 " + dxljstr+"\n";
                    }
                    if(dxcc>0)
                    {
                        counts += "导线拆除  合计："+dxcc+"米，其中 " +dxccstr+"\n";
                    }
                }
                if(lxaz>0)
                {
                    counts += "("+j+")拉线情况：\n";
                    j++;
                    counts += "拉线安装  合计："+(int)lxaz+"根，其中 "+ lxazstr+"\n";
                }


            }
            if(myContentsbList.size()>0) {

                counts += i+" 配网设备\n";
                i++;
                counts += "（1）设备汇总\n";
                int n=1;
                for(String sbname :listsbname) {
                    counts +=  n+")"+sbname +"情况： \n";
                    n++;
                    double sbxz=0;
                    String  sbxzstr="";
                    double sblj=0;
                    String  sbljstr="";
                    double sbcc=0;
                    String  sbccstr="";
                    double sbxl=0;
                    String  sbxlstr="";

                    for (MyContent myContent : myContentsbList) {
                        if (myContent.getpName().equals(aPRangeEntity.getPRName())
                                && myContent.getpType().equals(sbname)
                                && myContent.getpPWorktype().equals("新装")) {
                            sbxzstr += myContent.getMx() + ";";
                            sbxz += myContent.getZl();
                        }
                        if (myContent.getpName().equals(aPRangeEntity.getPRName())
                                && myContent.getpType().equals(sbname)
                                && myContent.getpPWorktype().equals("利旧")) {
                            sbljstr += myContent.getMx() + ";";
                            sblj += myContent.getZl();
                        }
                        if (myContent.getpName().equals(aPRangeEntity.getPRName())
                                && myContent.getpType().equals(sbname)
                                && myContent.getpPWorktype().equals("拆除")) {
                            sbccstr += myContent.getMx() + ";";
                            sbcc += myContent.getZl();
                        }
                        if (myContent.getpName().equals(aPRangeEntity.getPRName())
                                && myContent.getpType().equals(sbname)
                                && myContent.getpPWorktype().equals("修理")) {
                            sbxlstr += myContent.getMx() + ";";
                            sbxl += myContent.getZl();
                        }
                    }
                    if(sbxz>0)
                    {
                        counts += sbname+"新装   合计："+(int)sbxz+"台，其中 "+ sbxzstr+"\n";
                    }
                    if(sblj>0)
                    {
                        counts += sbname+"利旧  合计："+(int)sblj+"台，其中 "+sbljstr+"\n";
                    }
                    if(sbxl>0)
                    {
                        counts += sbname+"修理  合计："+(int)sbxl+"台，其中 "+sbxlstr+"\n";
                    }
                    if(sbcc>0)
                    {
                        counts += sbname+"拆除  合计："+(int)sbcc+"台，其中 "+sbccstr+"\n";
                    }

                }




                counts += "（2）设备明细\n";
                int m=1;
                for(EWorkContentEntity eWorkContentEntity:dWorkContentList)
                {
                    counts+=   m+")"+eWorkContentEntity.getWCContent()+";\n";
                    m++;
                }

            }


        }
        return counts;
    }


    public void setexportsimg(Document document, BaseFont bfChinese, EPBaseEntity ePBase) throws DocumentException, IOException {


        //表题字体
        Font titleFont = new Font(bfChinese, 11, Font.BOLD);
        //表格字体
        Font contextFont = new Font(bfChinese, 8, Font.NORMAL);
        // 签字字体
        Font qinFont = new Font(bfChinese, 10, Font.NORMAL);
        Paragraph title = new Paragraph((ePBase.getPBName() == null ? "" : ePBase.getPBName()) + "项目验收照片");
        title.setAlignment(Element.ALIGN_CENTER);
        title.setFont(titleFont);
        document.add(title);


        Map<String, Object> mapapole = new HashMap<>();
        String polewheres = " 1=1 ";
        polewheres += " and  p_guid = '" + ePBase.getGuid() + "' ";
        polewheres += " and  p_p_datastate!= '删除' ";
        mapapole.put("filterSql", polewheres + " ");
        List<EPointEntity> ePointList = ePointService.queryList(mapapole);

        Map<String, Object> mapCost = new HashMap<>();
        String mapCostwheres = " 1=1 ";
        mapCostwheres += " and  p_guid = '" + ePBase.getGuid() + "' ";
        mapCostwheres += " and  c_c_datastate!= '删除' ";
        mapCost.put("filterSql", mapCostwheres + " ");
        List<ECrossCostEntity> eCrossCostList = eCrossCostService.queryList(mapCost);

        addpoleimg(document, ePBase, contextFont, ePointList);
        addcrossimg(document, ePBase, contextFont, eCrossCostList);

    }

    private void addcrossimg(Document document, EPBaseEntity ePBase, Font contextFont, List<ECrossCostEntity> eCrossCostList) throws DocumentException, IOException {
        for (ECrossCostEntity eCrossCostEntity : eCrossCostList) {
            Map<String, Object> mapaPOther = new HashMap<>();
            String mapaPOtherwheres = " 1=1 ";
            mapaPOtherwheres += " and  p_guid = '" + ePBase.getGuid() + "' ";
            mapaPOtherwheres += " and  p_status = '验收' ";
            mapaPOtherwheres += " and  p_p_guid = '" + eCrossCostEntity.getGuid().toString() + "' ";
            mapaPOtherwheres += " and  p_datastate != '删除' ";
            mapaPOther.put("filterSql", mapaPOtherwheres + " ");
            List<DPictureEntity> dPictureList = dPictureService.queryList(mapaPOther);
            Map<String, Object> mapproblemOther = new HashMap<>();
            String mapproblemOtherwheres = " 1=1 ";
            mapproblemOtherwheres += " and  projectguid = '" + ePBase.getGuid() + "' ";
            mapproblemOtherwheres += " and  pointguid = '" + eCrossCostEntity.getGuid().toString() + "' ";
            mapproblemOtherwheres += " and  datastate != '删除' ";
            mapproblemOther.put("filterSql", mapproblemOtherwheres + " ");
            List<EProblemexistEntity> eProblemexistList = eProblemexistService.queryList(mapproblemOther);


            Table table = new Table(6);
            int[] withs = {1, 2, 1, 2, 1, 2};
            table.setWidths(withs);
            /** 表格所占页面宽度 author:yyli Sep 15, 2010 */
            table.setWidth(90);
            /** 居中显示 author:yyli Sep 15, 2010 */
            table.setAlignment(Element.ALIGN_CENTER);
            /** 自动填满 author:yyli Sep 15, 2010 */
            table.setAutoFillEmptyCells(true);
            EPRangeEntity ePRangeEntity=	ePRangeService.queryObject(eCrossCostEntity.getPRGuid());

            OutPutUtils.addcell(table, bDataDictionaryService.selectvaluebykey("cross_type", eCrossCostEntity.getCType()) + "(" +ePRangeEntity.getPRName()+"_"+ eCrossCostEntity.getCCName() + ")", 0, 6, true, contextFont);
            if (eProblemexistList.size() == 0) {
                OutPutUtils.addcell(table, "验收问题", 0, 0, true, contextFont);
                OutPutUtils.addcell(table, "", 0, 5, false, contextFont);

            } else {
                OutPutUtils.addcell(table, "验收问题", eProblemexistList.size(), 0, true, contextFont);
                for (EProblemexistEntity eProblemexistEntity : eProblemexistList) {
                    OutPutUtils.addcell(table, eProblemexistEntity.getCommon(), 0, 5, false, contextFont);
                }
            }

            if (dPictureList.size() > 0) {
                int chishu = dPictureList.size() / 3;
                int yushu = dPictureList.size() % 3;

                if (yushu > 0) {
                    chishu = chishu + 1;
                }
                for (int i = 0; i < chishu; i++) {
                    boolean one = false;
                    boolean two = false;
                    boolean three = false;

                    if ((3 * i) < dPictureList.size()) {
                        one = true;
                    }
                    if (((3 * i) + 1) < dPictureList.size()) {
                        two = true;
                    }
                    if (((3 * i) + 2) < dPictureList.size()) {
                        three = true;
                    }
                    if (one) {
                        File file = new File(ROOT + dPictureList.get(3 * i).getPServeraddress());
                        if (file.exists()) {
                            OutPutUtils.addcellimg(table, ROOT + dPictureList.get(3 * i).getPServeraddress(), 8, 2, true, contextFont);
                        } else {
                            OutPutUtils.addcell(table, "没找到照片", 8, 2, true, contextFont);

                        }
                    } else {
                        OutPutUtils.addcell(table, "", 8, 2, true, contextFont);
                    }
                    if (two) {
                        File file = new File(ROOT + dPictureList.get((3 * i) + 1).getPServeraddress());
                        if (file.exists()) {
                            OutPutUtils.addcellimg(table, ROOT + dPictureList.get((3 * i) + 1).getPServeraddress(), 8, 2, true, contextFont);
                        } else {
                            OutPutUtils.addcell(table, "没找到照片", 8, 2, true, contextFont);

                        }
                    } else {
                        OutPutUtils.addcell(table, "", 8, 2, true, contextFont);
                    }
                    if (three) {
                        File file = new File(ROOT + dPictureList.get((3 * i) + 2).getPServeraddress());
                        if (file.exists()) {
                            OutPutUtils.addcellimg(table, ROOT + dPictureList.get((3 * i) + 2).getPServeraddress(), 8, 2, true, contextFont);
                        } else {
                            OutPutUtils.addcell(table, "没找到照片", 8, 2, true, contextFont);

                        }
                    } else {
                        OutPutUtils.addcell(table, "", 8, 2, true, contextFont);
                    }


                    OutPutUtils.addcell(table, "名称", 0, 0, true, contextFont);
                    if (one) {
                        OutPutUtils.addcell(table, dPictureList.get(3 * i).getPLabel(), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }
                    OutPutUtils.addcell(table, "名称", 0, 0, true, contextFont);
                    if (two) {
                        OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPLabel(), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }
                    OutPutUtils.addcell(table, "名称", 0, 0, true, contextFont);
                    if (three) {
                        OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPLabel(), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }

                    OutPutUtils.addcell(table, "来源", 0, 0, true, contextFont);
                    if (one) {
                        OutPutUtils.addcell(table, dPictureList.get(3 * i).getPPResour(), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }
                    OutPutUtils.addcell(table, "来源", 0, 0, true, contextFont);
                    if (two) {
                        OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPPResour(), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }
                    OutPutUtils.addcell(table, "来源", 0, 0, true, contextFont);
                    if (three) {
                        OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPPResour(), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }
                    OutPutUtils.addcell(table, "时间", 0, 0, true, contextFont);
                    if (one) {
                        OutPutUtils.addcell(table, dPictureList.get(3 * i).getPTime() == null ? "" : DateUtils.format(dPictureList.get(3 * i).getPTime(), "yyyy-MM-dd"), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }
                    OutPutUtils.addcell(table, "时间", 0, 0, true, contextFont);
                    if (two) {
                        OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPTime() == null ? "" : DateUtils.format(dPictureList.get((3 * i) + 1).getPTime(), "yyyy-MM-dd"), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }
                    OutPutUtils.addcell(table, "时间", 0, 0, true, contextFont);
                    if (three) {
                        OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPTime() == null ? "" : DateUtils.format(dPictureList.get((3 * i) + 2).getPTime(), "yyyy-MM-dd"), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }


                }

            }
            if (eProblemexistList.size() + dPictureList.size() > 0) {
                document.add(table);
            }

        }
    }

    private void addpoleimg(Document document, EPBaseEntity ePBase, Font contextFont, List<EPointEntity> ePointList) throws DocumentException, IOException {
        for (EPointEntity ePointEntity : ePointList) {
            Map<String, Object> mapaPOther = new HashMap<>();
            String mapaPOtherwheres = " 1=1 ";
            mapaPOtherwheres += " and  p_guid = '" + ePBase.getGuid() + "' ";
            mapaPOtherwheres += " and  p_status = '验收' ";
            mapaPOtherwheres += " and  p_p_guid = '" + ePointEntity.getGuid().toString() + "' ";
            mapaPOtherwheres += " and  p_datastate != '删除' ";
            mapaPOther.put("filterSql", mapaPOtherwheres + " ");
            List<DPictureEntity> dPictureList = dPictureService.queryList(mapaPOther);
            Map<String, Object> mapproblemOther = new HashMap<>();
            String mapproblemOtherwheres = " 1=1 ";
            mapproblemOtherwheres += " and  projectguid = '" + ePBase.getGuid() + "' ";
            mapproblemOtherwheres += " and  pointguid = '" + ePointEntity.getGuid().toString() + "' ";
            mapproblemOtherwheres += " and  datastate != '删除' ";
            mapproblemOther.put("filterSql", mapproblemOtherwheres + " ");
            List<EProblemexistEntity> eProblemexistList = eProblemexistService.queryList(mapproblemOther);

            Table table = new Table(6);
            int[] withs = {1, 2, 1, 2, 1, 2};
            table.setWidths(withs);
            /** 表格所占页面宽度 author:yyli Sep 15, 2010 */
            table.setWidth(90);
            /** 居中显示 author:yyli Sep 15, 2010 */
            table.setAlignment(Element.ALIGN_CENTER);
            /** 自动填满 author:yyli Sep 15, 2010 */
            table.setAutoFillEmptyCells(true);
            EPRangeEntity ePRangeEntity=	ePRangeService.queryObject(ePointEntity.getPRGuid());

            OutPutUtils.addcell(table, ePointEntity.getPPCode() + "(" +ePRangeEntity.getPRName()+"_"+ ePointEntity.getPPName() + ")", 0, 6, true, contextFont);

            if (eProblemexistList.size() == 0) {
                OutPutUtils.addcell(table, "验收问题", 0, 0, true, contextFont);
                OutPutUtils.addcell(table, "", 0, 5, false, contextFont);

            } else {
                OutPutUtils.addcell(table, "验收问题", eProblemexistList.size(), 0, true, contextFont);
                for (EProblemexistEntity eProblemexistEntity : eProblemexistList) {
                    OutPutUtils.addcell(table, eProblemexistEntity.getCommon(), 0, 5, false, contextFont);
                }
            }


            if (dPictureList.size() > 0) {

                int chishu = dPictureList.size() / 3;
                int yushu = dPictureList.size() % 3;

                if (yushu > 0) {
                    chishu = chishu + 1;
                }
                for (int i = 0; i < chishu; i++) {
                    boolean one = false;
                    boolean two = false;
                    boolean three = false;

                    if ((3 * i) < dPictureList.size()) {
                        one = true;
                    }
                    if (((3 * i) + 1) < dPictureList.size()) {
                        two = true;
                    }
                    if (((3 * i) + 2) < dPictureList.size()) {
                        three = true;
                    }
                    if (one) {
                        File file = new File(ROOT + dPictureList.get(3 * i).getPServeraddress());
                        if (file.exists()) {
                            OutPutUtils.addcellimg(table, ROOT + dPictureList.get(3 * i).getPServeraddress(), 8, 2, true, contextFont);
                        } else {
                            OutPutUtils.addcell(table, "没找到照片", 8, 2, true, contextFont);

                        }
                    } else {
                        OutPutUtils.addcell(table, "", 8, 2, true, contextFont);
                    }
                    if (two) {
                        File file = new File(ROOT + dPictureList.get((3 * i) + 1).getPServeraddress());
                        if (file.exists()) {
                            OutPutUtils.addcellimg(table, ROOT + dPictureList.get((3 * i) + 1).getPServeraddress(), 8, 2, true, contextFont);
                        } else {
                            OutPutUtils.addcell(table, "没找到照片", 8, 2, true, contextFont);

                        }
                    } else {
                        OutPutUtils.addcell(table, "", 8, 2, true, contextFont);
                    }
                    if (three) {
                        File file = new File(ROOT + dPictureList.get((3 * i) + 2).getPServeraddress());
                        if (file.exists()) {
                            OutPutUtils.addcellimg(table, ROOT + dPictureList.get((3 * i) + 2).getPServeraddress(), 8, 2, true, contextFont);
                        } else {
                            OutPutUtils.addcell(table, "没找到照片", 8, 2, true, contextFont);

                        }
                    } else {
                        OutPutUtils.addcell(table, "", 8, 2, true, contextFont);
                    }


                    OutPutUtils.addcell(table, "名称", 0, 0, true, contextFont);
                    if (one) {
                        OutPutUtils.addcell(table, dPictureList.get(3 * i).getPLabel(), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }
                    OutPutUtils.addcell(table, "名称", 0, 0, true, contextFont);
                    if (two) {
                        OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPLabel(), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }
                    OutPutUtils.addcell(table, "名称", 0, 0, true, contextFont);
                    if (three) {
                        OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPLabel(), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }

                    OutPutUtils.addcell(table, "来源", 0, 0, true, contextFont);
                    if (one) {
                        OutPutUtils.addcell(table, dPictureList.get(3 * i).getPPResour(), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }
                    OutPutUtils.addcell(table, "来源", 0, 0, true, contextFont);
                    if (two) {
                        OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPPResour(), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }
                    OutPutUtils.addcell(table, "来源", 0, 0, true, contextFont);
                    if (three) {
                        OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPPResour(), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }
                    OutPutUtils.addcell(table, "时间", 0, 0, true, contextFont);
                    if (one) {
                        OutPutUtils.addcell(table, dPictureList.get(3 * i).getPTime() == null ? "" : DateUtils.format(dPictureList.get(3 * i).getPTime(), "yyyy-MM-dd"), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }
                    OutPutUtils.addcell(table, "时间", 0, 0, true, contextFont);
                    if (two) {
                        OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPTime() == null ? "" : DateUtils.format(dPictureList.get((3 * i) + 1).getPTime(), "yyyy-MM-dd"), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }
                    OutPutUtils.addcell(table, "时间", 0, 0, true, contextFont);
                    if (three) {
                        OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPTime() == null ? "" : DateUtils.format(dPictureList.get((3 * i) + 2).getPTime(), "yyyy-MM-dd"), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }


                }

            }
            if (eProblemexistList.size() + dPictureList.size() > 0) {
                document.add(table);
            }
        }
    }

}
